package com.sl.au.repository;

import com.sl.au.entity.PayPlan;
import com.sl.au.entity.PaymentInitial;
import com.sl.au.entity.PaymentPlan;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
//import org.springframework.data.jpa.repository.query.Procedure;
//import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import java.util.Date;
import java.util.List;

/**
 * 回款历史
 * Created by RX on 2016-11-29.
 */
@Transactional(readOnly = true)
public interface PayPlanRepository extends JpaRepository<PayPlan, String> {
//    @Procedure(name = "payNotionalPooling")
//    void payNotionalPooling(@Param("contractID") String contractID);
    /**
     * 获取回款-计划数据
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PayPlan p where p.contractId = ?1 and p.contractType=?2 order by p.createTime asc ")
    Page<PayPlan> findByContractIdAndContractType(String contractId, String contractType,Pageable pageable);
    /**
     * 得到回款-计划数据条数
     * @param contractId
     * @return
     */
    @Query(" select count(*) as num from PayPlan p where p.contractId = ?1")
    Long getPayPlanCount(String contractId);

    /**
     * 获取回款-计划数据表中第一条数据
     * @return
     */
    @Query("select p from PayPlan p where p.contractId = ?1 and p.contractType= ?2 order by p.createTime desc ")
    List<PayPlan> getNewPayPlan(String contractId,String contractType);

    /**
     * 获得修改回款的回款-计划记录
     * @param id
     * @return
     */
    @Query("select p from PayPlan p where p.payHistoryId = ?1 order by p.createTime asc ")
    List<PayPlan> getchangePayPlan(String id);
    /**
     * 删除修改的回款之后的回款-计划记录
     * @param changeTime
     */
    @Transactional
    @Modifying
    @Query("delete from PayPlan p where p.contractId=?1 and p.createTime>=?2")
    void deleteChange(String contractId,Date changeTime);

    /**
     * 获取回款-计划数据表中第一条数据
     * @param contractId
     * @return
     */
    @Query("select p from PayPlan p where p.contractType = ?1 order by p.createTime desc ")
    List<PayPlan> getNewsPayPlan(String contractId);
    /**
     * 删除修改的回款之后的回款-计划记录
     * @param createTime
     */
    @Transactional
    @Modifying
    @Query("delete from PayPlan p where p.contractType=?1 and p.createTime>=?2")
    void updataChange(String contractId, Date createTime);

    /**
     * 获取需要更新的计划
     * @param contractId
     * @param fund
     * @return
     */
    @Query("select p from PayPlan p where p.contractId = ?1 and p.fund=?2 ")
    PayPlan getupdataSchedule(String contractId, String fund);

    /**
     * 删除所有回款计划
     * @param contractId
     */
    @Transactional
    @Modifying
    @Query("delete from PayPlan p where p.contractId=?1")
    void deleteBycontractId(String contractId);
}
